package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// OperationLog 操作日志
type OperationLog struct {
	ID            string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	OperationType int    `json:"operation_type" gorm:"column:operation_type;type:int;size:32;index:idx_association,priority:1;comment:关联类型"`
	ObjID         string `json:"obj_id" gorm:"column:obj_id;type:string;size:30;index:idx_association,priority:2;comment:对象ID"`
	AssociationID string `json:"association_id" gorm:"column:association_id;type:string;size:30;index:idx_association,priority:3;comment:关联id"`
	HistoryData   string `json:"history_data" gorm:"column:history_data;type:string;size:300;comment:历史数据"`
	CurrentData   string `json:"current_data" gorm:"column:current_data;type:string;size:300;comment:当前数据"`
	UserName      string `json:"user_name" gorm:"column:user_name;type:string;size:20;comment:操作人"`
	CreatedAt     int64  `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间"`
}

// TableName 表名称
func (OperationLog) TableName() string {
	return "operation_log"
}

// BeforeCreate 自增id
func (l *OperationLog) BeforeCreate(tx *gorm.DB) (err error) {
	if l.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	l.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &OperationLog{})
}
